package com.github.zj.dreamly.guava.concurrent.test;

import com.github.zj.dreamly.guava.concurrent.Bucket;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

import static java.lang.Thread.currentThread;

/**
 * @author 苍海之南
 */
public class BucketExample {

	public static void main(String[] args) {
		final Bucket bucket = new Bucket();
		final AtomicInteger DATA_CREATOR = new AtomicInteger(0);

		IntStream.range(0, 5).forEach(i ->
		{
			new Thread(() ->
			{
				for (; ; ) {
					int data = DATA_CREATOR.getAndIncrement();
					bucket.submit(data);
					try {
						TimeUnit.MILLISECONDS.sleep(200L);
					} catch (Exception e) {
						if (e instanceof IllegalStateException) {
							System.out.println(e.getMessage());
						}
					}
				}

			}).start();
		});

		IntStream.range(0, 5)
			.forEach(i -> new Thread(() ->
				{
					for (; ; ) {
						bucket.takeThenConsume(x -> System.out.println(currentThread() + " W " + x));
					}
				}).start()
			);
	}
}
